﻿package  {
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.filters.BlurFilter;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	import com.greensock.easing.CustomEase;
	import flash.utils.getTimer;

	
	public class Wheel extends MovieClip {
		//params
		public var icon_width:Number = 128;
		public var icon_height:Number = 128;
		public var icon_space:Number = 0;
		public var icon_num:int = 15;
		//onclip
		public var icons_mask:Shape;
		public var icons_holder:MovieClip;
		//contents
		public var icons:Array;
		public var current:Array;
		//events
		public var onStart:Function = function(){};
		public var onFast:Function = function(){};
		public var onSlow:Function = function(){};
		public var onStop:Function = function(){};
		//workspace
		private var tick_timer:Timer;
		private var ease1;
		private var ease2;
		private var tms:TweenMax;
		private var wheelTime:int = 1;
		private var startTime:int;

		
		public function Wheel() {
			super();
			
			this.icons = new Array();
			this.current = new Array();
			this.icons_holder = new MovieClip();
			this.icons_mask = new Shape();  
			this.icons_mask.graphics.beginFill(0x000000);  
			this.icons_mask.graphics.drawRect(0,0, this.icon_width,this.icon_height*3+this.icon_space*2);
			this.icons_mask.graphics.endFill();  
			this.icons_holder.mask = this.icons_mask;
			this.addChild(this.icons_holder);
			this.addChild(this.icons_mask);
			//create_icons;
			this.icons_holder.y = (this.icon_height+this.icon_space)*4;//last4
			var i:int; //15 icons
			var a:Array = new Array();
			for(i=0;i<this.icon_num;i++)
			{
				this.icons.push(new WIcon(Helper.rand(1,12,a)));
				a.push(this.icons[i].current);
				if(a.length > 2) { a.shift(); }
				this.icons[i].x = 0;
				this.icons[i].y = (i-this.icon_num)*(this.icon_height+this.icon_space);
				this.icons_holder.addChild(this.icons[i]);
			}
			
			this.current = new Array(this.icons[this.icon_num-4].current,this.icons[this.icon_num-3].current,this.icons[this.icon_num-2].current);
			
			this.ease1 = CustomEase.create("myCustomEase1",[{s:0,cp:-0.09,e:0.16},{s:0.16,cp:0.41,e:1}]);
			this.ease2 = CustomEase.create("myCustomEase3", [{s:0,cp:0.646,e:0.896},{s:0.896,cp:1.14599,e:1}]);
			
			this.tick_timer = new Timer(32);
			this.tick_timer.addEventListener(TimerEvent.TIMER, this.wheelFastTimer);
		}

		private function wheelStart():void
		{
			this.startTime = getTimer()+800;
			this.icons_holder.y = (this.icon_height+this.icon_space)*4; //last4
			this.setBlur(1,2);
			//LEAK
			TweenMax.to(this.icons_holder,.8,{y:((this.icon_height+this.icon_space)*11), ease:this.ease1, onComplete:wheelFast});
			this.onStart();
		}
		
		private function wheelFast():void
		{
			this.icons_holder.y = (this.icon_height+this.icon_space)*4;
			this.setBlur(60,2);
			this.tick_timer.delay = 32;
			this.tick_timer.start();
			this.onFast()
		}
		private function wheelFastTimer(e:TimerEvent):void
		{
			var t:Number = (this.icon_height+this.icon_space)*4;
			this.icons[this.icon_num-5].showIcon(Helper.rand(1,12));
			this.icons[this.icon_num-4].showIcon(Helper.rand(1,12,[this.icons[this.icon_num-5].current]));
			this.icons[this.icon_num-3].showIcon(Helper.rand(1,12,[this.icons[this.icon_num-5].current,this.icons[this.icon_num-4].current]));
			this.icons[this.icon_num-2].showIcon(Helper.rand(1,12,[this.icons[this.icon_num-4].current,this.icons[this.icon_num-3].current]));
			this.icons[this.icon_num-1].showIcon(Helper.rand(1,12,[this.icons[this.icon_num-3].current,this.icons[this.icon_num-2].current]));
			
			this.icons_holder.y = this.icons_holder.y+4;
			if(this.icons_holder.y > (t+40))
			{
				this.icons_holder.y = t;
			}
			//check stop;
			if(getTimer() >= (this.startTime+this.wheelTime))
			{
				this.wheelFastStop();
			}
		}
		
		private function wheelFastStop():void
		{
			this.tick_timer.stop();
			
			this.icons_holder.y = (this.icon_height+this.icon_space)*4; //last4
			this.setBlur(0,0);
			this.icons[1].showIcon(this.current[0]);
			this.icons[2].showIcon(this.current[1]);
			this.icons[3].showIcon(this.current[2]);
			//LEAK
			TweenMax.to(this.icons_holder,.8,{y:((this.icon_height+this.icon_space)*14), ease:this.ease2, onComplete:wheelStopped});
			this.onSlow();
		}
		
		private function wheelStopped():void
		{
			this.tick_timer.stop();
			
			this.setBlur(0,0);
			this.icons[this.icon_num-5].showIcon(this.icons[0].current);
			this.icons[this.icon_num-4].showIcon(this.icons[1].current);
			this.icons[this.icon_num-3].showIcon(this.icons[2].current);
			this.icons[this.icon_num-2].showIcon(this.icons[3].current);
			this.icons[this.icon_num-1].showIcon(this.icons[4].current);
			this.icons_holder.y = (this.icon_height+this.icon_space)*4; //last4
			this.onStop();
		}
		
		//PUBLICS
		public function drawIcons(simbs:Array):void
		{
			this.current = simbs;
			//stopAny
			this.tick_timer.stop();
			
			this.setBlur(0,0);
			this.icons_holder.y = (this.icon_height+this.icon_space)*4;//last4
			var i:int; //15 icons
			var a:Array = new Array();
			for(i=0;i<this.icon_num;i++)
			{
				this.icons[i].showIcon(Helper.rand(1,12,a));
				a.push(this.icons[i].current);
				if(a.length > 2) { a.shift(); }
			}
			delete(a); //ANTI LEAK
			delete(i); //ANTI LEAK
			this.icons[this.icon_num-4].showIcon(this.current[0]);
			this.icons[this.icon_num-3].showIcon(this.current[1]);
			this.icons[this.icon_num-2].showIcon(this.current[2]);
		}
		
		public function playWheel(time:int = 0, _onStart:Function = null, _onFast:Function = null, _onSlow:Function = null, _onStop:Function = null):void
		{
			//stopAny
			this.tick_timer.stop();

			this.setBlur(0,0);
			//assign functions;
			this.onStart = function(e=null){};
			this.onFast = function(e=null){};
			this.onSlow = function(e=null){};
			this.onStop = function(e=null){};
			if(_onStart != null) this.onStart = _onStart;
			if(_onFast != null) this.onFast = _onFast;
			if(_onSlow != null) this.onSlow = _onSlow;
			if(_onStop != null) this.onStop = _onStop;
			//startIt
			if(time!=0)
				this.wheelTime = time;
			else 
				this.wheelTime = 600*1000; //infinite loop (10 minutes);
				
			this.wheelStart();
		}
		
		public function stopWheel(interval:int = 0):void
		{
			if(getTimer() < this.startTime)
			{
				this.wheelTime = interval;
			} else {
				this.wheelTime = interval + (getTimer()-this.startTime);
			}
		}

		
		
		
		
		
		
		//inner
		private function setBlur(len:Number,pow:Number):void
		{
			var myBlur:BlurFilter = new BlurFilter(0,len,pow);
			this.icons_holder.filters = [myBlur];
		}
	}
	
}
